﻿using AutoMapper;
using Azure;
using KayakMes.Common;
using KayakMes.Common.Dtos.ProcessDTO.Input;
using KayakMes.Common.Dtos.ProcessDTO.Output;
using KayakMes.Common.Result;
using KayakMes.ProcessManagement.Domain.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.ProcessManagement.Application.Process
{
    public class TechnologicalFlowService : ITechnologicalFlowService
    {
        private readonly BaseReposiotry<TechnologicalFlowModel> _technologicalFlowModel;
        private readonly IMapper _mapper;

        public TechnologicalFlowService(BaseReposiotry<TechnologicalFlowModel> technologicalFlowModel, IMapper mapper)
        {
            _technologicalFlowModel = technologicalFlowModel;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增工艺信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> Add(TechnologicalFlowInput input)
        {
            var entity = _mapper.Map<TechnologicalFlowModel>(input);
            entity.Id = YitIdHelper.NextId();

            var res = _technologicalFlowModel.Add(entity);

            if (res>0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "添加工艺信息成功",
                    Data = res
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "添加工艺信息失败",
                    Data = 0
                };
            }
        }
        /// <summary>
        /// 根据Id查询工艺信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<TechnologicalFlowOutput> FindTechnologicalFlow(long id)
        {
            var query = _technologicalFlowModel.GetModel(id);

            var entity = _mapper.Map<TechnologicalFlowOutput>(query);

            return new ApiResult<TechnologicalFlowOutput>()
            {
                Code = ApiEnums.Success,
                Data = entity,
                Message = "查询工艺信息成功"
            };  
        }

        /// <summary>
        /// 查询工艺信息列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiPaging<TechnologicalFlowOutput> GetTechnologicalFlowList(GetTechnologicalFlowInput input)
        {
            var query = _technologicalFlowModel.GetAll().Where(x=>!x.IsDeleted);

            if (!string.IsNullOrEmpty(input.TechnoloigcalCode))
            {
                query = query.Where(x=>x.TechnologicalCode .Contains( input.TechnoloigcalCode));
            }

            if (!string.IsNullOrEmpty(input.TechnoloigcalName))
            {
                query = query.Where(x => x.TechnologicalName .Contains( input.TechnoloigcalName));
            }

            if (input.IsEnabled != null)
            {
                query = query.Where(x => x.IsEnabled == input.IsEnabled);
            }

            var total = query.Count();

            var list = query.OrderBy(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();

            var entity = _mapper.Map<List<TechnologicalFlowOutput>>(list);

            return new ApiPaging<TechnologicalFlowOutput>
            {
                Items = entity,
                Total = total,
                Code = ApiEnums.Success,
                Message = "获取成功",
                PageIndex = input.PageIndex,
                PageSize = input.PageSize,
            };

        }

        /// <summary>
        /// 根据ID删除工序设置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteTechnologicalFlow(long id)
        {
            var query = _technologicalFlowModel.GetModel(id);
            if (query != null && id > 0)
            {
                var res = _technologicalFlowModel.Delete(query);

                if (res > 0)
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Success,
                        Data = res,
                        Message = "删除成功"
                    };
                }
                else
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Data = res,
                        Message = "删除失败"
                    };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Data = -1,
                    Message = "找不到要删除的信息！"
                };
            }
        }

        /// <summary>
        /// 修改工序设置信息
        /// </summary>
        /// <param name="output"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateTechnologicalFlow(TechnologicalFlowOutput output)
        {
            var query = _technologicalFlowModel.GetModel(output.Id);

            if (query != null && output.Id > 0)
            {
                var entity = _mapper.Map(output, query);

                var res = _technologicalFlowModel.Update(entity);

                if (res > 0)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Message = "更新成功",
                        Data = res
                    };
                }
                else
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Data = res,
                        Message = "更新失败"
                    };
                }
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Data = -1,
                    Message = "找不到要修改的信息！"
                };
            }
        }

    }
}
